<template>

  <v-card :title="title" @reset="reset" @submit="submit">
    <v-form slot="form" ref="form" :rules="rules" :form-title="formTitle" :form-data="formData"/>
  </v-card>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.formData.pwdNew !== '') {
          console.log(this.$refs.form)
          this.$refs.form.$refs.form.validateField('pwdNewTwo')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.formData.pwdNew) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        pwdNew: [
          { validator: validatePass, trigger: 'blur' }
        ],
        pwdNewTwo: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      title: '编辑',
      roleList: [],
      formData: {}
    }
  },
  computed: {
    formTitle() {
      return ([
        // {
        //   en: 'id',
        //   cn: 'id',
        //   disabled: true
        // },
        {
          en: 'username',
          cn: '用户名',
          // disabled:true
        },
        {
          en: 'name',
          cn: '名称'
        },
        {
          en: 'role_id',
          cn: '角色',
          type: 'select',
          list: this.roleList
        },
        {
          en: 'pwdNew',
          cn: '密码',
          type: 'password'
        },
        {
          en: 'pwdNewTwo',
          cn: '重复密码',
          type: 'password'
        },
        {
          cn: '创建时间',
          en: 'created_at',
          disabled: true
        },
        {
          cn: '更新时间',
          en: 'updated_at',
          disabled: true
        }

      ])
    },
  },
  activated() {
    this.getRoleList()
    // 接收传参
    const data = this.$route.params.data
    if (data) {
      localStorage.setItem('details', JSON.stringify(data))
      this.formData = this.$route.params.data
    }

    this.formData = localStorage.getItem('details') && JSON.parse(localStorage.getItem('details')) || {}

    this.$emit('childTitle', this.$route.meta.title1)
  },
  destroyed() {
    localStorage.removeItem('details')
  },
  methods: {

    // 获取角色列表
    getRoleList() {
      this.$get('action/PositionCtrl/postionTree', {})
        .then(res => {
          this.roleList = res.data.map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
        })
    },

    back() {
      this.$router.go(-1)
    },

    // 撤销
    reset() {
      this.formData = localStorage.getItem('details') && JSON.parse(localStorage.getItem('details')) || {}
    },
    submit() {
      const data = {
        name: this.formData.name,
        id: this.formData.id,
        username: this.formData.username,
        roleId: this.formData.role_id
      }
      if (this.formData.pwdNew && this.formData.pwdNewTwo) {
        data.pwdNew = this.formData.pwdNew
        data.pwdNewTwo = this.formData.pwdNewTwo
      }

      this.$get('action/user/savePwd', data)
        .then(res => {
          setTimeout(() => this.$router.go(-1), 1000)
          this.$message({ message: '修改成功', type: 'success' })
        })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">

</style>
